# 主窗口视图 
from PyQt6.QtWidgets import QMainWindow, QWidget, QHBoxLayout, QStackedWidget
from PyQt6.QtGui import QIcon
from .components.sidebar import Sidebar
from .home_page import HomePage
from .contract_mgmt_page import ContractMgmtPage
from .budget_mgmt_page import BudgetMgmtPage
from .export_mgmt_page import ExportMgmtPage
from .companies_mgmt_page import CompaniesMgmtPage
from .view_setting import MainWindow_Config

class MainWindow(QMainWindow):
    """主窗口"""
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        # 设置窗口基本属性
        self.setWindowTitle(MainWindow_Config["WINDOW_TITLE"])
        # 设置窗口图标
        self.setWindowIcon(QIcon(MainWindow_Config["WINDOW_ICON"]))
        # 设置窗口最小尺寸
        self.setMinimumSize(*MainWindow_Config["WINDOW_SIZE"])
        # 设置窗口默认最大化
        if MainWindow_Config["WINDOW_MAXIMIZED"]:
            self.showMaximized()
        
        # 创建中心部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建水平布局
        layout = QHBoxLayout(central_widget)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 创建侧边栏
        self.sidebar = Sidebar()
        self.sidebar.page_changed.connect(self.on_page_changed)
        
        # 创建堆叠部件用于页面切换
        self.stack = QStackedWidget()
        self.stack.setStyleSheet("""
            QStackedWidget {
                background-color: #F3F6F9;
            }
        """)
        
        # 创建并添加各个页面
        self.pages = {
            'homePage': HomePage(),
            'contract': ContractMgmtPage(),
            'budget': BudgetMgmtPage(),
            'export': ExportMgmtPage(),
            'companies': CompaniesMgmtPage(),
        }
        
        # 添加所有页面到堆叠部件
        for page in self.pages.values():
            self.stack.addWidget(page)
        
        # 将侧边栏和堆叠部件添加到布局中
        layout.addWidget(self.sidebar)
        layout.addWidget(self.stack)
        
        # 设置窗口样式
        self.setStyleSheet("""
            QMainWindow {
                background-color: #F3F6F9;
            }
        """)
        
    def on_page_changed(self, page_name):
        """处理页面切换事件"""
        if page_name in self.pages:
            self.stack.setCurrentWidget(self.pages[page_name])
